﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace ZHDJ.Data.Models.Common
{
    /// <summary>
    /// 积分规则
    /// </summary>
    public class PartyObjectScoreRule : EntityBase
    {
        /// <summary>
        /// 积分名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 积分类型
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// Member人员，Branch组织
        /// </summary>
        public string ObjectType { get; set; }
        /// <summary>
        /// 积分
        /// </summary>
        public decimal Score { get; set; }
        /// <summary>
        /// 每个小时间内只能获得的积分上限
        /// </summary>
        public decimal HourMax { get; set; }

        /// <summary>
        /// 每天内只能获得的积分上限
        /// </summary>
        public decimal DayMax { get; set; }

        /// <summary>
        /// 每周内只能获得的积分上限
        /// </summary>
        public decimal WeekMax { get; set; }

        /// <summary>
        /// 每个月内只能获得的积分上限
        /// </summary>
        public decimal MonthMax { get; set; }

        /// <summary>
        /// 每年内只能获得的积分上限
        /// </summary>
        public decimal YearMax { get; set; }

        /// <summary>
        /// 状态1 禁用  4启用
        /// </summary>
        public int Status { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 修改人
        /// </summary>
        public string ModifyBy { get; set; }


        #region 查询管理

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="status"></param>
        /// <param name="searchKey"></param>
        /// <returns></returns>
        public static DataSet GetGridData(int pageIndex, int pageSize, string status, string searchKey = "",string objectType="")
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS  data.*,
                        case data.Status when 4 then '启用' else '禁用' end as  StatusName ,
case data.ObjectType when 'Branch' then '组织积分' else '个人积分' end as  ObjectTypeName
                        from tblPartyObjectScoreRule data
                        where 1=1 ");
            if (!string.IsNullOrEmpty(searchKey))
            {
                sb.Append(" and data.Name like ?key ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "key", Value = string.Format("%{0}%", searchKey) });
            }

            if (!string.IsNullOrEmpty(status))
            {
                sb.Append(" and data.status=?status");
                parameters.Add(new MySqlParameter() { DbType = DbType.Int32, ParameterName = "status", Value = status });
            }
            if (!string.IsNullOrEmpty(objectType))
            {
                sb.Append(" and data.objectType=?objectType");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "objectType", Value = objectType });
            }
            sb.Append(" order by data.Category LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }


        #endregion
    }
}
